
package com.jd.dl.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanLocator {
    
    private static Log log = LogFactory.getLog(BeanLocator.class);
    
	private static ApplicationContext context;

	static {
		init();
	}

	public static void init() {
		 try {
			String[] springConfigs = new String[3];
			springConfigs[0] = "Hbm-Context.xml";
			springConfigs[1] = "DAO-Context.xml";
			springConfigs[2] = "Service-Context.xml";

			if (log.isDebugEnabled()) {
				log.debug("springConfigs:");
				for (int i=0;i<springConfigs.length;i++) {
					log.debug(springConfigs[i]);
				}
			}
//			System.setProperty("javax.xml.parsers.DocumentBuilderFactory", 
//					"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
			context = new ClassPathXmlApplicationContext(springConfigs);

        } catch (Exception e) {
            log.error("context not in class path...", e);
             try {
            } catch (BeansException e1) {
                log.error("context not found...", e);
            }
        }
	}

	public static Object getBean(String beanName) {
		return context.getBean(beanName);
	}
	
}
